﻿using Axiom.CodeManifold.Framework;
using EnvDTE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Axiom.CodeManifold
{
	public static class DocumentExtensions
	{
		public static Project GetProject(this Document doc)
		{
			if (doc == null)
			{
				throw new NoActiveDocumentException("No Active Document");
			}

			return doc.ProjectItem.ContainingProject;
		}

		public static void AutoComment(this Document doc)
		{
			if (doc == null)
			{
				return;
			}

			var item = doc.ProjectItem.FileCodeModel.CodeElements.Item(1);
			var line = item.StartPoint.Line;
			if (line == 1)
			{
				var edit = item.StartPoint.CreateEditPoint();
				edit.Insert(Commenter.DefaultComment);
			}

			var classes = doc.Entities<CodeClass>();
			foreach (var @class in classes)
			{
				@class.AutoComment();
			}
		}

		public static IList<T> Entities<T>(this Document doc) where T : class
		{
			return doc.ProjectItem.FileCodeModel.GetEntities<T>();
		}
	}

	public static class Commenter
	{
		private static string _defaultComment = null;

		static Commenter()
		{
			_defaultComment =
				string.Format(@"//==================================================================================={0}",Environment.NewLine) +
				string.Format(@"// {0} Development{1}", ReferenceData.CompanyName, Environment.NewLine) +
				string.Format(@"//==================================================================================={0}",Environment.NewLine) +
				string.Format(@"// THIS CODE AND INFORMATION ARE PROVIDED ""AS IS"" WITHOUT WARRANTY OF ANY KIND, {0}",Environment.NewLine) +
				string.Format(@"// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES {0}",Environment.NewLine) +
				string.Format(@"// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.{0}",Environment.NewLine) +
				string.Format(@"//==================================================================================={0}",Environment.NewLine) +
				string.Format(@"// Copyright (c) {0} Corporation.  All Rights Reserved.{1}", ReferenceData.CompanyName, Environment.NewLine) +
				string.Format(@"// This code is released under the terms of the MS-LPL license, {0}",Environment.NewLine) +
				string.Format(@"// {0}{1}", ReferenceData.SoftwareLicenseUrl, Environment.NewLine) +
				string.Format(@"//==================================================================================={0}",Environment.NewLine) +
				string.Format(Environment.NewLine);
		}

		public static string DefaultComment
		{
			get
			{
				return _defaultComment;
			}
		}
	}
}
